<template>
	<view>
		<view class="background1 colonn">
			<view class="p-all-20 roww" style="color: red;font-weight: bold;" >
				发货单号：{{orderInfo.orderNo}}
			</view>
			<view class="roww p-all-25 border_bottom">
				<view>条形码：</view>
				<view class="w-20"></view>
				<input placeholder="请输入条形码" class="allline" v-model="tiaoxingma" />
				<view @click="tiaoxingmaTo()">查询</view>
			</view>
			<view class="colonn p-all-30" v-if="scanInfo">
				<view style="line-height: 50rpx">商品名称：{{ scanInfo.goodsName }}</view>
				<view class="h-20"></view>
				<view>条码：{{ scanInfo.barcode }}</view>
				<view class="h-20"></view>
				<view class="roww">
					<view>本次发货数量：{{ scanInfo.deliverNum }}</view>
				</view>
				<view class="h-20"></view>
				<view class="roww center_center">
					<view>实际到货数量：</view>
					<view style="border: 1px solid #f5f5f5; padding: 0rpx 20rpx">
						<input
							v-model="scanInfo.sjNum"
							class="w-200"
							style="color: red; font-weight: bold; text-align: center; height: 100rpx; font-size: 35rpx"
							placeholder="请输入数量"
						/>
					</view>
					<view>{{ scanInfo.unitName }}</view>
					<view class="allline"></view>
				</view>
				<view class="h-20"></view>
			</view>
			<view v-else class="m-top-50 m-bottom-50 roww center_center">
				<view>请扫码查询商品</view>
			</view>
			<view class="colonn colonn">
				<view class="roww center_center">
					<view class="btnss11" @click.stop="toSumSubmit">提交</view>
				</view>
			</view>
		</view>

		<view class="colonn center_center">
			<view class="h-20"></view>
			<view class="roww">
				<view>发货单商品列表</view>
			</view>

			<!-- applyNum  申请发货数量
				alreadyDeliverNum    实际出库数量
				alreadyInNum 已入库数量
				deliverNum  本次发货数量
				distributionNum 分配数量	系统自动或手动分配的可发货库存数 -->

			<view class="h-20"></view>
			<view class="colonn background1 w-750 p-all-25 m-bottom-20" v-for="(item, index) in goodList">
				<view class="roww">
					<view class="w-710">商品名称：{{ item.goodsName }}</view>
				</view>
				<view class="h-20"></view>
				<view class="roww">
					<view>商品条形码：{{ item.barcode }}</view>
				</view>
				<view class="h-20"></view>
				<view class="roww">
					<view>本次发货数量：{{ item.deliverNum }}</view>
				</view>
				<view class="h-20"></view>
				<view class="roww center_center">
					<view>实际到货：</view>
					<view style="border: 1px solid #f5f5f5; padding: 0rpx 20rpx">
						<input
							v-model="item.sjNum"
							class="w-200"
							style="color: red; font-weight: bold; text-align: center; height: 100rpx; font-size: 35rpx"
							placeholder="请输入数量"
						/>
					</view>
					<view class="w-20"></view>
					<view>{{ item.unitName }}</view>
					<view class="allline"></view>
				</view>
			</view>
		</view>

		<view class="saobtn" @click.stop="onScan">扫</view>
		
		<view class="saobtn2 colonn center_center" @click.stop="saveDate()">
			<view>
				暂存
				<br />
				数据
			</view>
		</view>
		
		<view class="saobtn1 colonn center_center" @click.stop="toReturn">
			<view>
				点击
				<br />
				完成
			</view>
		</view>
	</view>
</template>

<script>
import wx from 'weixin-js-sdk';
export default {
	data() {
		return {
			tiaoxingma: '',
			scanInfo: null,

			orderInfo: {},
			oorder: {},
			goodList: []
		};
	},
	onLoad(options) {
		this.getSxSign();
		this.yanzhengApiWx();
		
		
		if(options.uptype&&options.uptype==1){
			this.getStoDate()
		}else{
			var orderInfo = uni.getStorageSync('orderInfo');
			this.orderInfo = orderInfo;
			this.getGoodList();
		}
		
		
		
		
		
	},
	methods: {
		
		// 保存缓存数据
		saveDate(){
			
			uni.setStorageSync("orderInfo_tiaoxingma",this.tiaoxingma);
			uni.setStorageSync("orderInfo_scanInfo",this.scanInfo);
			uni.setStorageSync("orderInfo_orderInfo",this.orderInfo);
			uni.setStorageSync("orderInfo_oorder",this.oorder);
			uni.setStorageSync("orderInfo_goodList",this.goodList);
			this.$tools.showToast("缓存成功");
			
		},
		
		getStoDate(){
			this.tiaoxingma=uni.getStorageSync('orderInfo_tiaoxingma');
			this.scanInfo=uni.getStorageSync('orderInfo_scanInfo');
			this.orderInfo=uni.getStorageSync('orderInfo_orderInfo');
			this.oorder=uni.getStorageSync('orderInfo_oorder');
			this.goodList=uni.getStorageSync('orderInfo_goodList');
		},
		
		
		// 去扫码
		onScan() {
			wx.scanQRCode({
				needResult: 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
				scanType: ['qrCode', 'barCode'], // 可以指定扫二维码还是一维码，默认二者都有
				success: (res1) => {
					this.tiaoxingma = res1.resultStr;
					this.tiaoxingmaTo();
				},
				fail: function (response) {},
				complete: (res) => {}
			});
		},

		getSxSign() {
			var uurl = window.location.href.split('#')[0];
			var data = {
				url: uurl
			};
			this.$axios
				.axios('get', this.$paths.getWxSignature, data)
				.then((res) => {
					wx.config({
						debug: false, // 开启调试模式,
						appId: res.corpid, // 必填，企业号的唯一标识
						timestamp: res.timestamp, // 必填，生成签名的时间戳
						nonceStr: res.noncestr, // 必填，生成签名的随机串
						signature: res.signature, // 必填，签名
						jsApiList: ['scanQRCode', 'checkJsApi', 'chooseImage', 'onMenushareTimeline', 'onMenushareAppMessage'] // 必填，需使用的JS接口列表
					});
					wx.ready((res) => {
						console.log('1配置完成，扫码前准备完成', res);
					});
					wx.error(function (res) {
						//wx.config配置错误，会弹出窗口哪里错误，然后根据微信文档查询即可。
						console.log('错误扫一扫', res);
					});
				})
				.catch((err) => {
					console.log('错误回调', err);
				});
		},
		// 验证api是否可用
		yanzhengApiWx() {
			wx.checkJsApi({
				jsApiList: ['scanQRCode', 'checkJsApi', 'chooseImage'], // 需要检测的JS接口列表
				success: function (res) {
					// 以键值对的形式返回，可用的api值true，不可用为false
					// 如：{"checkResult":{"chooseImage":true},"errMsg":"checkJsApi:ok"}
				},
				complete: (res) => {
					console.log('complete', res);
				}
			});
		},

		isSubmit() {
			var goodList = this.goodList;
			var goodnames = [];
			var goodsTrue = [];
			var isnoNum = true;
			for (var a = 0; a < goodList.length; a++) {
				if (goodList[a].sjNum == '' || !goodList[a].sjNum) {
					isnoNum = false;
				}
				if (goodList[a].sjNum > goodList[a].deliverNum) {
					goodnames.push(goodList[a].goodsName);
				}
				if (goodList[a].sjNum == goodList[a].deliverNum) {
					goodsTrue.push(goodList[a].goodsName);
				}
			}
			if (!isnoNum) {
				this.$tools.showToast('部分商品实际到货数量未填写，请仔细检查再提交');
				return false;
			}
			if (goodnames.length > 0) {
				this.$tools.showToast(goodnames.join(',') + '这些商品实际到货数量超过发货数量请检查之后再提交');
				return false;
			}
			return true;
		},

		toReturn() {
			if (!this.isSubmit()) {
				return false;
			}

			var goodList = this.goodList;
			var goodsTrue = [];
			for (var a = 0; a < goodList.length; a++) {
				if (goodList[a].sjNum == goodList[a].deliverNum) {
					goodsTrue.push(goodList[a].goodsName);
				}
			}
			if (goodsTrue.length == goodList.length) {
				var data = {
					ordering: this.orderInfo,
					gooding: this.goodList
				};
				
				var userInfowx=uni.getStorageSync("userInfowx");
				console.log('userInfowx',userInfowx)
				if(userInfowx){
					data.username=userInfowx.user.nickName
				}
				
				this.$axios
					.axios('post', this.$paths.allrukuPost, data)
					.then((res) => {
						if (res.code == 200) {
							this.$tools.showToast('到货无差异，入库成功');
							setTimeout((res) => {
								uni.reLaunch({
									url: '/pages/index/index'
								});
							}, 1000);
						}else{
							this.$tools.showToast(res.msg);
						}
					})
					.catch((err) => {
						console.log('错误回调', err);
					});
				return false;
			}

			uni.setStorageSync('ordering', this.orderInfo);
			uni.setStorageSync('gooding', this.goodList);
			
			
			// this.removeDate();
			uni.navigateTo({
				url: '/pages/returnOrder/returnOrder'
			});
		},

		toSumSubmit() {
			if (!this.scanInfo) {
				this.$tools.showToast('请先扫码获取商品');
				return false;
			}
			if (this.scanInfo.sjNum == '') {
				this.$tools.showToast('请输入到货数量');
				return false;
			}
			var goodList = this.goodList;
			for (var a = 0; a < goodList.length; a++) {
				if (goodList[a].barcode == this.scanInfo.barcode) {
					this.goodList[a] = this.scanInfo;
					console.log(this.goodList[a]);
					break;
				}
			}
			this.$forceUpdate();
			this.scanInfo = null;
		},

		tiaoxingmaTo() {
			var goodList = this.goodList;
			for (var a = 0; a < goodList.length; a++) {
				if (goodList[a].barcode == this.tiaoxingma) {
					this.scanInfo = goodList[a];
					return;
					break;
				}
			}
			this.$tools.showToast('不在此发货单中');
		},

		getGoodList() {
			var data = this.orderInfo;
			data['channelId'] = uni.getStorageSync('shopInfo')['channel_id'];
			data['channelName'] = uni.getStorageSync('shopInfo')['channel_name'];
			this.$axios
				.axios('post', this.$paths.getSendCargoorderGoods, data)
				.then((res) => {
					if (res.code == 200) {
						this.oorder = res.result.data;
						var goodList = res.result.data.posSupplyInGoodsDtoArr;
						for (var a = 0; a < goodList.length; a++) {
							goodList[a].sjNum = ''; //实际到的数量
							goodList[a].posunnum = 0; //破损的数量
							goodList[a].chayi = 0; //少的数量
							goodList[a].chayiIs = 1; //1不差异
							goodList[a].posunIs = 1; //1不破损
							goodList[a].imgPs = ''; //// 破损图片
							goodList[a].reasonPs = ''; // 破损备注
							goodList[a].imgKt = ''; //// 空退图片
							goodList[a].reasonKt = ''; // 空退备注
							goodList[a].selTxtKt = ''; // 空退选项内容
							goodList[a].selTxtPs = ''; // 破损选项内容
						}
						this.goodList = goodList;
					}
				})
				.catch((err) => {
					console.log('错误回调', err);
				});
		}
	}
};
</script>

<style>
@import url('orderInfo.css');
</style>
